4 I know the algorithm is fast enough (it's O(m * n)) but the implementation is slow
27 template <class T
> string
toStr(const T
&x
){ stringstream s
; s
<< x
; return s
.str(); }
28 template <class T
> int toInt(const T
&x
){ stringstream s
; s
<< x
; int r
; s
>> r
; return r
; }
30 #define For(i, a, b) for (int i=(a); i<(b); ++i)
31 #define foreach(x, v) for (typeof (v).begin() x = (v).begin(); x != (v).end(); ++x)
32 #define D(x) cout << #x " = " << (x) << endl
34 string
next(const string
&s
);
35 string
prev(const string
&s
);
37 bool looksLike0000(const string
&s
){
38 for (int i
=0; i
<s
.size(); ++i
) if (s
[i
] != '0') return false;
42 bool looksLike1000(const string
&s
){
43 return s
[0] == '1' && looksLike0000(s
.substr(1));
46 string
next(const string
&s
){
47 if (s
.size() == 1) return s
== "0" ? "1" : "0";
48 string t
= s
.substr(1);
50 if (looksLike1000(t
)){
56 if (looksLike0000(t
)){
64 string
prev(const string
&s
){
65 if (s
.size() == 1) return s
== "0" ? "1" : "0";
66 string t
= s
.substr(1);
68 if (looksLike0000(t
)){
74 if (looksLike1000(t
)){
85 while (cin
>> m
>> s
&& m
){
86 while (m
--) s
= next(s
);